package lrscp.lib.io.Monitor;

import lrscp.lib.Common;

public class DisplayConsoleWait implements Runnable {
    private String message;
    private char waitChar;
    private Thread mThd;
    private boolean exit = false;
    private long interval;
    private int charCount = 5;

    public DisplayConsoleWait(String mesg, char waitChar, int charCount, long flashInterval) {
        message = mesg;
        this.waitChar = waitChar;
        interval = flashInterval;
        this.charCount = charCount;
        mThd = new Thread(this);
        mThd.start();
    }

    @Override
    public void run() {
        int count = 0;
        while (!exit) {
            System.out.print("\r" + message);
            for (int i = 0; i < charCount; i++) {
                if (i <= count) {
                    System.out.print(waitChar);
                } else {
                    System.out.print(' ');
                }
            }
            count++;
            if (count >= charCount) {
                count = 0;
            }
            Common.sleep(interval);
        }
    }

    public void exit() {
        exit = true;
        try {
            mThd.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] argv) {
        new DisplayConsoleWait("hello", '.', 8, 500);
    }
}
